package com.origin.src.mybatis;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInterceptor;
import com.origin.src.ApplicationTool;
import com.origin.src.datasource.DataSourceBean;
import com.origin.src.mybatis.interceptor.PwdInterceptor;
import com.origin.src.mybatis.mapper.TUserMapper;
import com.origin.src.mybatis.pojo.TUser;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import javax.sql.DataSource;

@ComponentScan({"com.origin.src.mybatis", "com.origin.src.datasource"})
@MapperScan(value = "com.origin.src.mybatis"
//        , annotationClass = Mapper.class
)
public class MyBatisApp {
    public static void main(String[] args) {
//        System.out.println(MyBatisApp.class.getMethods()[1].getDeclaringClass());
        app();
    }



    public static void mybatis() {
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("myBatis", transactionFactory, DataSourceBean.dataSource());
        Configuration configuration = new Configuration(environment);
        configuration.addMapper(TUserMapper.class);
        SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);
        TUserMapper tUserMapper = configuration.getMapper(TUserMapper.class, sqlSessionFactory.openSession());
        System.out.println(tUserMapper.selectByPrimaryKey(1L));

    }

    public static void app() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyBatisApp.class);
        ApplicationTool.printBean(app);
        TUserMapper tUserMapper = app.getBean(TUserMapper.class);
        TUser tUser = new TUser();
        tUser.setUserId(1L);
        tUser.setUserName("test1");
        tUser.setUserEmail("222@222.com");
        tUser.setUserPhone("180000000");
        tUser.setLoginPassword("明文");
        System.out.println(tUserMapper.insert(tUser));
        tUser.setUserName("name-" + tUser.getUserId());
        System.out.println(tUserMapper.updateByPrimaryKey(tUser));
        System.out.println(tUserMapper.selectAll());
//        System.out.println(tUserMapper.deleteByPrimaryKey(tUser.getUserId()));
    }

    @Autowired
    PwdInterceptor pwdInterceptor;

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(DataSource dataSource) {
        TransactionFactory transactionFactory = new SpringManagedTransactionFactory();

        Environment environment = new Environment("myBatis", transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);
//        configuration.addInterceptor(pwdInterceptor);
        configuration.addInterceptor(new PageInterceptor());
        configuration.setLogImpl(StdOutImpl.class);
        SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
        System.out.println(sqlSessionTemplate.getExecutorType());
        return sqlSessionTemplate;

    }

}
